#include "client.h"

static int get_ser_fd();
int token_login(){
    int num = 4;
    int netFD = get_ser_fd();
    send(netFD, &num, sizeof(num), MSG_NOSIGNAL);
    send(netFD, global_token, sizeof(global_token), MSG_NOSIGNAL);

    int re_num;
    recv(netFD, &re_num, sizeof(int), MSG_WAITALL);
    if(re_num == -1){
        return -1;
    }
    
    return netFD;
}

static int get_ser_fd(){
    int ser_fd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(ser_fd,-1,"socket");

    struct sockaddr_in ser_addr;
    ser_addr.sin_family = AF_INET;
    int ret = inet_pton(AF_INET,"127.0.0.1",&ser_addr.sin_addr);
    ERROR_CHECK(ret,-1,"inet_pton");
    ser_addr.sin_port = htons(atoi("8080"));

    ret = connect(ser_fd,(struct sockaddr*)&ser_addr,sizeof(ser_addr));
    ERROR_CHECK(ret,-1,"connect");

    return ser_fd;
}
